package com.neuedu.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.neuedu.service.NeueduPayService;
import com.neuedu.util.NeueduPayConfig;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class NeueduPayServiceImpl implements NeueduPayService {
    @Override
    public String neueduPay(String registerNum, BigDecimal registMoney, String orderName) throws AlipayApiException {
        // 创建客户端
        AlipayClient client = new DefaultAlipayClient(NeueduPayConfig.GATEWAYURL, NeueduPayConfig.APPID, NeueduPayConfig.PRIVATEKEY, NeueduPayConfig.FORMAT, NeueduPayConfig.CHARSET, NeueduPayConfig.ALIPAYPUBLICKEY, NeueduPayConfig.SIGNTYPE);
        // 创建请求
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setReturnUrl("http://127.0.0.1:81/register"); // 支付成功后跳转的页面
        request.setNotifyUrl("http://uydtzq.natappfree.cc/register/updatePay");  // 内网穿透 支付成功后异步通知更新挂号状态
        final JSONObject jsonObject = new JSONObject();
        jsonObject.put("out_trade_no", registerNum);
        jsonObject.put("total_amount", registMoney.toString());
        jsonObject.put("subject", orderName);
        jsonObject.put("body", orderName);
        jsonObject.put("product_code", "FAST_INSTANT_TRADE_PAY");
        request.setBizContent(jsonObject.toJSONString());
        // // 调用客户端发送请求
        return client.pageExecute(request).getBody();
    }
}
